package com.zlp.alipay.util;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.zlp.alipay.dto.pay.AlipayBean;
import com.zlp.alipay.dto.pay.PayRefundBean;
import com.zlp.alipay.service.PayRefundService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 支付宝支付工具类
 * @className AliPayUtil
 * @date 2020/5/12 10:47
 */
@Component
@Slf4j(topic = "AliPayUtil")
public class AliPayUtil {

    @Resource
    private AlipayProperties alipayProperties;
    @Resource
    private PayRefundService payRefundService;


    private String serverUrl;
    private String appId;
    private String privateKey ;
    private String format = "json";
    private String charset;
    private String alipayPublicKey;
    private String signType;
    private String returnUrl;
    private String notifyUrl;

    /**
     * 支付接口
     *
     * @param alipayBean 封装的支付宝入参
     * @return 返回支付结果
     * @throws AlipayApiException 抛出异常
     */
    public String pay(AlipayBean alipayBean) throws AlipayApiException {

        // 1、获得初始化的AlipayClient
        init();

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径 html
        alipayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径接口
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付页面结果
        String body = alipayClient.pageExecute(alipayRequest).getBody();
        return body;
    }

    /**
     * 支付接口
     *
     * @param payRefundBean 封装的支付宝入参
     * @return 返回支付结果
     * @throws AlipayApiException 抛出异常
     */
    public String payRefund(PayRefundBean payRefundBean) {

        // 1、获得初始化的AlipayClient
        init();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(payRefundBean));
        String result = null;
        AlipayTradeRefundResponse execute = null;
        try {
            execute = alipayClient.execute(alipayRequest);
            log.info(JSON.toJSONString(execute));
        } catch (Exception ex) {
            log.error("支付宝退款错误！error={}", ex.getMessage());
        }finally {
            if(Objects.nonNull(execute) && execute.getCode().equals("10000")){
                result = execute.getBody();
                payRefundService.updateRefundRecord(payRefundBean.getOut_request_no(),result,2);
            }else {
                payRefundService.updateRefundRecord(payRefundBean.getOut_request_no(),result,1);
            }
        }
        return result;
    }


    /**
     * 初始化配置信息
     * @date: 2021/11/4 17:53
     */
    private void init(){

        serverUrl = alipayProperties.getGatewayUrl();
        appId = alipayProperties.getAppId();
        privateKey = alipayProperties.getPrivateKey();
        charset = alipayProperties.getCharset();
        alipayPublicKey = alipayProperties.getPublicKey();
        signType = alipayProperties.getSignType();
        returnUrl = alipayProperties.getReturnUrl();
        notifyUrl = alipayProperties.getNotifyUrl();
    }


}
